<?php
// Connect to the database
$dbLink = new mysqli('localhost', 'root', "", 'db_name');
if(mysqli_connect_errno()) {$dbLink = new mysqli('localhost', 'root', "", 'db_name');
    die("MySQL connection failed: ".mysqli_connect_error());
}
// Query for a list of all existing files
$sql = 'SELECT  `text_id`, `text`, `author_username`, `date_added` FROM  `texts`
    ORDER BY `text_id` DESC';
$result = $dbLink->query($sql);
// Check if it was successfull
if($result) {
    
    // Make sure there are some files in the database
    if($result->num_rows == 0) {
        echo '<p>There are no posts in the database</p>';
    }
    else {
        // Print the top of a table
        echo '<table width="100%">
                <tr>
                    <td><b>user</b></td>
                    <td><b>says</b></td>
                    <td><b>Created</b></td>
                    
                </tr>';
        // Print each file
        while($row = $result->fetch_assoc()) {
            echo "
                <tr>
                    <td>{$row['author_username']}</td>
                    <td>{$row['text']}</td>
                    <td>{$row['date_added']}</td>
                    </tr>"; 
        }
        // Close table
        echo '</table>';
    }
    // Free the result
    $result->free();
}
else
{
    // if connetion to database failed
    echo 'Error! SQL query failed:';
    echo "<pre>{$dbLink->error}</pre>";
}
// Close the mysql connection
$dbLink->close();
//  link to return back to the main page
echo '<p>Click <a href="index.php">here</a> to go back</p>';
?>